package com.example.test01.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.test01.R;
import com.example.test01.bean.Planet;

import java.util.List;

public class PlanetBaseAdapter extends BaseAdapter {


    private Context mContext;
    private List<Planet> mPlanetList;

    //创建构造方法
    public PlanetBaseAdapter(Context mContext, List<Planet> mPlanetList) {
        this.mContext = mContext;
        this.mPlanetList = mPlanetList;
    }




    //获得列表项的个数
    @Override
    public int getCount() {
        return mPlanetList.size();
    }

    //获取
    @Override
    public Object getItem(int position) {
        return mPlanetList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if(convertView == null){
            //根据布局文件item_list.xml生成视图对象
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list,null);
            holder = new ViewHolder();
            holder.iv_icon = convertView.findViewById(R.id.iv_icon);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_desc = convertView.findViewById(R.id.tv_desc);

            //将试图持有者保存到转换器是视图中
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder)convertView.getTag();
        }




        Planet plant = mPlanetList.get(position);
        holder.iv_icon.setImageResource(plant.image);
        holder.tv_name.setText(plant.name);
        holder.tv_desc.setText(plant.desc);



        return convertView;
    }

    public final class ViewHolder{
        public ImageView iv_icon;
        public TextView tv_name;
        public TextView tv_desc;
    }



}
